from flask import request
from . import stu_bp
from router import conn
from lib.response import response

@stu_bp.route("/stu",methods=["GET"])
def get_stu_info():
    cursor = conn.cursor()
    cursor.execute("select * from stuinfo")
    data = cursor.fetchall()
    print(data)
    cursor.close()
    conn.commit()
    return response(data=data,message="get stu info success!")

@stu_bp.route("/stu/add",methods=["POST","GET","PUT"])
def add_stu():
    stu_name = request.json.get("stu_name")
    stu_age = request.json.get("stu_age")
    stu_class = request.json.get("stu_class")
    stu_address = request.json.get("stu_address")

    sql_str = (f"insert into stuinfo(stu_name,stu_age,stu_address,stu_class) "
               f"values('{stu_name}','{stu_age}','{stu_address}','{stu_class}')")

    cursor = conn.cursor()
    cursor.execute(sql_str)
    conn.commit()
    cursor.close()
    return response(message="add student info successgul!")

@stu_bp.route("/stu/modify",methods=["PUT"])
def modify_stu():
    return "this is modify_stu"

@stu_bp.route("/stu/delete",methods=["DELETE"])
def delete_stu():
    id = request.args.get("id")
    sql_str = f"delete from stuinfo where id = {id}"
    cursor = conn.cursor()
    cursor.execute(sql_str)
    conn.commit()
    cursor.close()
    return "delete student info successgul!"